1886C - Decreasing String - CodeForces Solution


strings strings

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define FOR(i,a,b) for (int i = (a), _b = (b); i <= _b; i++)
#define FOD(i,b,a) for (int i = (b), _a = (a); i >= _a; i--)
#define FORE(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); i++)
#define ALL(v) (v).begin(), (v).end()
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(i) (1LL << (i))
#define CNTBIT(x) __builtin_popcount(x)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define left ___left
#define right ___right
#define pii pair<int, int>
#define DEBUG(n, a) FOR (i, 1, n) cout << a[i] << ' '; cout << endl;1234567890
#define lob lower_bound // >=
#define upb upper_bound // >
#define endl "\n"

using namespace std;

template<class X, class Y> bool maximize(X &x, Y y){ if (x < y){ x = y; return true; } return false; }
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y){ x = y; return true; } return false; }

#define  gogobruhbruh  ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define  file(name)  if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }

const int MOD = 123456789;

void add(int &a, int b){ if ((a += b) >= MOD) a -= MOD; }
int sub(int a, int b){ if ((a -= b) < 0) a += MOD; return a; }
int muti(int a, int b){ return (1LL * a * b) % MOD; }
int Pow(int x, int y){ int res = 1; for (; y; y >>= 1){ if (y & 1) res = muti(res, x); x = muti(x, x); } return res; }

const int MAX = 1e6 + 5;
const int INF = 1e9 + 7;

string s;
ll pos;
int len, L[MAX], R[MAX];
priority_queue<int> heap;
bool ok[MAX];

int Find(int u, bool t){
    if (t)
        return ok[L[u]] ? L[u] = Find(L[u], t) : L[u];
    return ok[R[u]] ? R[u] = Find(R[u], t) : R[u];
}

void solve(){
    ll sum = 0;
    int x = 0;
    len = s.size();
    s = " " + s;
    for (int i = len; sum + i < pos; sum += i, i--)
        x++;
    s[0] = 'a' - 1;
    s[len + 1] = 'z' + 1;
    FOR (i, 1, len){
        L[i] = i - 1; R[i] = i + 1;
        if (s[i + 1] < s[i])
            heap.push(-i);
    }
    int cnt = 0;
//    cout << x << endl;
    while (heap.size()){
        int i = heap.top(); heap.pop();
        i *= -1;
//        cout << i << endl;
        if (cnt == x)
            continue;
        cnt++;
        ok[i] = 1;
        R[Find(i, 1)] = Find(i, 0);
        if (s[Find(i, 0)] < s[Find(i, 1)])
            heap.push(-Find(i, 1));
    }
    vector<pii> V;
    if (cnt < x){
        FOR (i, 1, len)
            if (!ok[i])
                V.pb({s[i], i});
        sort(ALL(V), greater<pii>());
        FOR (i, 0, x - cnt - 1)
            ok[V[i].se] = 1;
    }
    x = 0;
    FOR (i, 1, len){
        if (ok[i]){
            ok[i] = 0;
            continue;
        }
        x++;
//        cout << i << endl;
        if (x == pos - sum)
            cout << s[i];
    }
    V.clear();
}

void read(){
    cin >> s >> pos;
}

int main(){
    gogobruhbruh;
    file("main");
    int test = 1;
    cin >> test;
    while (test--)
        read(),
        solve();
}


Comments

Submit
0 Comments
More Questions

561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image
1295. Find Numbers with Even Number of Digits
1704. Determine if String Halves Are Alike
1732. Find the Highest Altitude
709. To Lower Case
1688. Count of Matches in Tournament
1684. Count the Number of Consistent Strings
1588. Sum of All Odd Length Subarrays
1662. Check If Two String Arrays are Equivalent
1832. Check if the Sentence Is Pangram
1678. Goal Parser Interpretation
1389. Create Target Array in the Given Order
1313. Decompress Run-Length Encoded List
1281. Subtract the Product and Sum of Digits of an Integer
1342. Number of Steps to Reduce a Number to Zero
1528. Shuffle String
1365. How Many Numbers Are Smaller Than the Current Number
771. Jewels and Stones
1512. Number of Good Pairs
672. Richest Customer Wealth
1470. Shuffle the Array
1431. Kids With the Greatest Number of Candies
1480. Running Sum of 1d Array
682. Baseball Game
496. Next Greater Element I